package com.plian.system.service.sys.fun;

import cn.hutool.core.util.ObjectUtil;
import com.plian.Tools.UuidUtil;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.dao.sys.fun.PerGroupDao;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.ObjResult;
import com.plian.system.model.page.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.exception.ExceptionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户操作
 */
@Slf4j
@Service
public class PerGroupService {
    @Autowired
    PerGroupDao perGroupDao;
    /**
     * 保存
     * @param p
     * @throws Exception
     */
    public ObjResult save(PageData p)  {
        ObjResult objResult;
        try {
            p.put("id",UuidUtil.get32UUID());
            PageData maxCode = perGroupDao.findMaxCode();
            if(null == maxCode || ObjectUtil.isEmpty(maxCode.get("code"))){
                p.put("code","001");
            }else {
                String max = (String) maxCode.get("code");
                int numericCode = Integer.parseInt(max);
                numericCode++;
                p.put("code",String.format("%03d", numericCode));
            }

            perGroupDao.save(p);
            objResult=new ObjResult(CommonCode.SUCCESS);
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            objResult=new ObjResult(CommonCode.ERROR);
        }
        return objResult;
    }
    /**
     * 修改
     * @param p
     * @throws Exception
     */
    public ObjResult edit(PageData p) {
        ObjResult objResult;
        try {
            perGroupDao.edit(p);
            objResult=new ObjResult(CommonCode.SUCCESS);
        }catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            objResult=new ObjResult(CommonCode.ERROR);
            return objResult;
        }
       return objResult;
    }

    /**
     * 根据id查询
     * @param p
     * @throws Exception
     */
    public ObjResult findbyid(PageData p) {
        ObjResult objResult;
        try {
            PageData p_user=perGroupDao.findbyid(p.getString("id"));
            objResult=new ObjResult(CommonCode.SUCCESS);
            objResult.setPageData(p_user);
        }catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            objResult=new ObjResult(CommonCode.ERROR);
            return objResult;
        }
        return objResult;
    }
    /**
     * 删除
     * @throws Exception
     */
    public ObjResult delids(List<String> list) {
        ObjResult objResult;
        try {
           perGroupDao.del_ids(list);
            objResult=new ObjResult(CommonCode.SUCCESS);
        }catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            objResult=new ObjResult(CommonCode.ERROR);
            return objResult;
        }
        return objResult;
    }

    /**
     * 用户列表分页
     * @param pd
     * @return
     */
    public ListResult<PageData> listpage(PageData pd){
        try {
            Page page=new Page(true,pd);
            List<PageData> list=perGroupDao.list(page);
            ListResult<PageData> Result=new ListResult<>(CommonCode.SUCCESS);
            Result.setList(list);
            Result.setTotal(page.getTotal());
            return Result;
        } catch (Exception e) {
            log.error(ExceptionUtils.getFullStackTrace(e));
            return  new ListResult<>(CommonCode.ERROR);
        }
    }



}
